var table;
var $;
var layer;
var form;
layui.use(['table', 'jquery', 'layer', 'form','laydate'], function () {
    table = layui.table;
    $ = layui.jquery;
    layer = layui.layer;
    form = layui.form;
    laydate = layui.laydate;
    /*选择事件*/
    laydate.render({
        elem: '#searchDate',
        type: 'date',
        trigger: 'click'
    });

    getJournal();
    //加载表格数据
    function getJournal() {
        $.ajax({
            url:'/goodsTable/getPriceJournal',
            type:'post',
            dataType:'json',
            data:{"type":1},
            success:function (data) {
                getTable(data.data);
            }
        });
    }
    function getTable(data) {
        var logTable = table.render({
            elem: "#root",
            data:data,
            toolbar: '#toptool',
            defaultToolbar: ['print', 'filter', 'exports'],
            limit: 10,
            page: {
                layout: ['count', 'prev', 'page', 'next', 'skip', 'refresh'],
                first: "首页",
                last: "尾页",
            },
            size: 'lg',
            cols: [[
                {type: 'checkbox', align: 'center', width: 60},
                {
                    type: 'numbers', title: '序号', align: 'center', width: 60, templet: function (data) {
                        return data.LAY_INDEX;
                    }
                },
                {field: 'productCode', title: '商品编号', align: 'center'},
                {field: 'courseName', title: '商品名称', align: 'center'},
                {field: 'coursePrice', title: '原价', align: 'center',templet:function (data) {
                        if (data.coursePrice == null){
                            data.coursePrice = '0';
                        }
                        return data.coursePrice+" 元";
                    }},
                {field: 'courseNprice', title: '现价', align: 'center',templet:function (data) {
                        if (data.courseNprice == null){
                            data.courseNprice = '0';
                        }
                        return data.courseNprice+" 元";
                    }},
                {field: 'updateDate', title: '操作时间', align: 'center'},
                {
                    title: '操作', align: 'center', templet: function (data) {
                        var tem = '<a  href="javascript:delPriceJournal(' + data.id + ');" style="color:rgb(13,171,158)">删除</a>';
                        return tem;
                    }
                },
            ]]
        })
    }

    /*删除一条记录*/
    delPriceJournal = function delPriceJournal(id) {
        layer.confirm('确定删除此条数据?', {
                icon: 3, title: '提示'
            },
            function (index) {
                $.ajax({
                    url: "/goodsTable/delPriceJournal",
                    method: "post",
                    dataType: "json",
                    data: {id: id},
                    success: function (data) {
                        if (data) {
                            layer.msg("删除成功");
                            layer.close(index);
                            getJournal();
                        } else {
                            layer.msg("删除失败");
                            layer.close(index);
                        }
                    }
                })
            }
        )
    }

    /*搜索*/
    form.on('submit(sub)', function (data) {
        $.ajax({
            url:'/goodsTable/getPriceJournal',
            type:'post',
            dataType:'json',
            data:{'productCode':data.field.searchCode,'productName':data.field.searchName,'updateDate':data.field.searchDate,"type":1},
            success:function (data) {
                getTable(data.data);
            }
        });
        return false;
    })
    /*重置*/
    $("#reset").click(function () {
        getJournal();
    })
})
/*刷新*/
function Refresh() {
    location.reload();
}

/*跳转*/
function goBack() {
    parent.$('#quickHref').val('商品列表');
    parent.getQuick();
    return;
}